package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbGroupActive;
import com.bwie.pojo.TbGroupJoin;
import com.bwie.service.TbGroupJoinService;
import com.bwie.mapper.TbGroupJoinMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Lenovo
* @description 针对表【tb_group_join(商家拼团活动表)】的数据库操作Service实现
* @createDate 2025-07-26 13:54:22
*/
@Service
public class TbGroupJoinServiceImpl extends ServiceImpl<TbGroupJoinMapper, TbGroupJoin>
    implements TbGroupJoinService{

    @Autowired
    TbGroupJoinMapper tbGroupJoinMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public R getGroupJoinPage(PageInfoVo pageInfoVo) {
        Page<TbGroupJoin> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        Page<TbGroupJoin> tbGroupJoinPage = tbGroupJoinMapper.getGroupJoinPage(page, pageInfoVo);

        for (TbGroupJoin record : tbGroupJoinPage.getRecords()) {
            List<String> imageList = tbGroupJoinMapper.getImageList(record.getJoinId());
            ArrayList<String> list = new ArrayList<>();
            if(imageList!=null && imageList.size()>0){
                for (String string : imageList) {
                    if(string!= null){
                        List<String> stringList = JSON.parseArray(string, String.class);
                        list.addAll(stringList);
                    }
                }
            }
            record.setJoinImageList(list);
        }
        return R.OK(tbGroupJoinPage);
    }

    @Override
    public R addGroupJoin(TbGroupJoin tbGroupJoin) {
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(404,"请先登录");
        }
        String userName = TokenUtil.userName(token);

        TbGroupJoin groupJoin = tbGroupJoinMapper.selectOne(
                new LambdaQueryWrapper<TbGroupJoin>().eq(TbGroupJoin::getJoinName, tbGroupJoin.getJoinName())
        );
        if(groupJoin != null){
            return R.ERROR(500,"该拼团活动已经存在");
        }

        long begin = tbGroupJoin.getBeginTime().getTime();
        long end = tbGroupJoin.getEndTime().getTime();
        if(begin>end){
            R.ERROR(500,"开始时间不能晚于截止时间");
        }

        tbGroupJoin.setJoinImages(JSON.toJSONString(tbGroupJoin.getJoinImageList()));
        tbGroupJoin.setCreateBy(userName);
        tbGroupJoin.setCreateTime(new Date());
//"[\"http://192.168.80.192:8888/group1/M00/00/03/wKhQwGiHGAWAc_YQABKOhCbECXQ116.jpg\",\"http://192.168.80.192:8888/group1/M00/00/03/wKhQwGiHGAyAaa3RABs7zHHyeWY455.jpg\"]"
        tbGroupJoinMapper.insert(tbGroupJoin);

        return R.OK();
    }

    @Override
    public R updateGroupJoin(TbGroupJoin tbGroupJoin) {
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(404,"请先登录");
        }
        String userName = TokenUtil.userName(token);

        long begin = tbGroupJoin.getBeginTime().getTime();
        long end = tbGroupJoin.getEndTime().getTime();
        if(begin>end){
            R.ERROR(500,"开始时间不能晚于截止时间");
        }

        tbGroupJoin.setJoinImages(JSON.toJSONString(tbGroupJoin.getJoinImageList()));
        tbGroupJoin.setUpdateBy(userName);
        tbGroupJoin.setUpdateTime(new Date());

        tbGroupJoinMapper.updateById(tbGroupJoin);

        return R.OK();
    }

    @Override
    public R deleteGroupJoin(Integer joinId) {
        TbGroupJoin tbGroupJoin = tbGroupJoinMapper.selectById(joinId);
        if(tbGroupJoin == null){
            return R.ERROR(404,"该团不存在");
        }

        tbGroupJoinMapper.deleteById(joinId);
        return R.OK();
    }
}




